package com.ggh.jinjilive.ui.home.live;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentLiveBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.create.StartLiveMobile;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.home.live.adapter.LiveAdapter;
import com.ggh.jinjilive.ui.home.live.adapter.LiveTypeAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.views.DividerItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class LiveFragment extends BaseFragment<FragmentLiveBinding> implements OnRefreshListener, OnLoadMoreListener {
    //直播间ViewModel
    private LiveViewModel liveViewModel;
    //适配器
    private LiveTypeAdapter liveTypeAdapter;
    //直播间适配器
    private LiveAdapter liveAdapter;
    //
    private GridLayoutManager mCommodityManager;
    //进入直播间
    private LiveList liveList;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        liveViewModel = new ViewModelProvider(this,viewModelFactory).get(LiveViewModel.class);

    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_live;
    }

    @Override
    public void initView() {
        super.initView();
        binding.setLiveViewModel(liveViewModel);

        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));

    }

    @Override
    public void initData() {
        super.initData();
        //类目
        liveTypeAdapter = new LiveTypeAdapter(mContext);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.liveTypeList.setLayoutManager(linearLayoutManager);
        binding.liveTypeList.setAdapter(liveTypeAdapter);
        liveTypeAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                LiveCategory liveCategory = (LiveCategory) mObject;
                if(null != liveCategory){
                    liveViewModel.liveCategory.set(liveCategory.getId());
                    onRefresh(binding.refreshLayout);
                }
            }
        });
        //直播间类目
        liveViewModel.liveCateGory();
        liveViewModel.listMutableLiveData.observe(this, new Observer<List<LiveCategory>>() {
            @Override
            public void onChanged(List<LiveCategory> liveCategories) {
                liveViewModel.liveCategory.set(liveCategories.get(0).getId());
                liveTypeAdapter.setAdd(liveCategories);
                onRefresh(binding.refreshLayout);
            }
        });
        //列表
        liveAdapter = new LiveAdapter(mContext);
        mCommodityManager = new GridLayoutManager(mContext, 2);
        mCommodityManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(position == 0){
                    return 2;
                }
                return 1;
            }
        });
        binding.recyclerView.setLayoutManager(mCommodityManager);
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(mContext));
        binding.recyclerView.setAdapter(liveAdapter);
        liveAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                if(position == 0){
                    liveViewModel.liveType.set(mObject.toString());
                    onRefresh(binding.refreshLayout);
                }else {
                    liveList = (LiveList) mObject;
                    if(null != liveList){
                        switch (liveList.getType()){
                            case 0://成功
                                //进入直播间
                                liveViewModel.theLiveView(liveList.getLive_id(),"");
                                break;
                            case 1://密码房

                                break;

                        }
                    }
                }
            }
        });
        //直播间列表
        liveViewModel.liveData.observe(this, new Observer<List<LiveList>>() {
            @Override
            public void onChanged(List<LiveList> liveLists) {
                binding.refreshLayout.finishRefresh();
                if(null != liveLists){
                    if(liveViewModel.isRefresh){
                        binding.refreshLayout.finishLoadMore();
                        liveLists.add(0,null);
                        liveAdapter.setAdd(liveLists);
                    }else {
                        if(liveLists.size() > 0){
                            liveAdapter.Add(liveLists);
                            binding.refreshLayout.finishLoadMore();
                        }else {
                            binding.refreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    }
                }else {
                    binding.refreshLayout.finishLoadMore();
                }

            }
        });
        //进入直播间 接口
        liveViewModel.mStartLiveMobile.observe(this, new Observer<StartLiveMobile>() {
            @Override
            public void onChanged(StartLiveMobile startLiveMobile) {
                //跳转到直播间
                ARouter.getInstance().build(ARouterUtils.AudienceLiveActivity)
                        .withSerializable("LiveList",liveList)
                        .withSerializable("StartLiveMobile",startLiveMobile)
                        .navigation();
            }
        });

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        liveViewModel.isRefresh = true;
        liveViewModel.liveList();
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        liveViewModel.isRefresh = false;
        liveViewModel.liveList();
    }


}
